package com.maosy.demo.channel

import android.annotation.SuppressLint
import android.content.Context
import android.widget.Toast
import com.maosy.demo.api.CmsApi
import com.maosy.demo.api.HttpRequest
import com.maosy.demo.api.ServerHost
import com.maosy.demo.util.gson
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

@SuppressLint("CheckResult")
object DemoMethodChannel {

    fun optionCall(
        methodChannel: MethodChannel,
        call: MethodCall,
        result: MethodChannel.Result,
        ctx: Context
    ) {
        when (call.method) {
            "toast" -> toast(ctx, result)
            "cmsBanner" -> bannerData(result)
        }
    }


    private fun toast(ctx: Context, result: MethodChannel.Result) {
        Toast.makeText(ctx, "this toast from native", Toast.LENGTH_SHORT).show()
        result.success("success")
    }

    private fun bannerData(result: MethodChannel.Result) {
        HttpRequest.getApi<CmsApi>(ServerHost.BASE_HOST).banner()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { result.success(gson.toJson(it)) }
    }

}